package com.yungu.swift.admin.route.order;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.order.model.param.AdminOrderUuidParam;
import com.yungu.swift.order.model.param.AdminRefundParam;
import com.yungu.swift.order.model.param.RefundParam;
import com.yungu.swift.order.service.RefundService;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : cuixiuyin
 * @date : 2019/7/18
 */
@Slf4j
@RestController
@RequestMapping("/admin/v1/order/refund")
@Api(value = "【订单管理】", tags = "订单退款操作")
public class OrderRefundAdmin {

    @Reference
    private RefundService refundService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "订单退款", httpMethod = "POST")
    public ResponseData<Boolean> orderRefund(@RequestBody AdminRefundParam adminRefundParam) {
        if (ParamUtil.isIllegal(adminRefundParam.getOrderUuid(), adminRefundParam.getPayFare(), adminRefundParam.getRemark())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        RefundParam refundParam = new RefundParam();
        BeanUtils.copyProperties(adminRefundParam, refundParam);
        refundParam.setCreateBy(adminRefundParam.getUserUuid());
        return ResponseData.buildSuccessResponse(refundService.refund(refundParam).isSuccess());
    }


    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "订单退款进度查询", httpMethod = "POST")
    public ResponseData<Boolean> query(@RequestBody AdminOrderUuidParam orderUuidParam) {
        if (ParamUtil.isIllegal(orderUuidParam.getOrderUuid())) {
            AdminErrorEnum paramError = AdminErrorEnum.PARAM_ERROR;
            return ResponseData.buildErrorResponse(paramError.getCode(), paramError.getMsg());
        }
        return refundService.refundQuery(orderUuidParam.getOrderUuid(), orderUuidParam.getUserUuid());
    }
}
